package com.mvc.service.impl;

import com.mvc.entity.Sku;
import com.mvc.entity.Spu;
import com.mvc.mapper.SkuMapper;
import com.mvc.mapper.SpuMapper;
import com.mvc.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: dior
 * @Package: com.mvc.service.impl
 * @ClassName: ProuctServiceImpl
 * @Description: TODO
 * @Author: Bang
 * @Date: 2021/12/1 17:08
 * @Version: 1.0
 **/
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    SpuMapper spuMapper;

    @Resource
    SkuMapper skuMapper;

    @Override
    public List<Spu> query(Map<String, Object> pars) {
        List<Spu> spuList = spuMapper.selectAll(pars);
        for (Spu spu : spuList) {
            spu.setSkuList(skuMapper.selectBySpuId(spu.getId()));
        }
        return spuList;
    }

    @Override
    public int count(Map<String, Object> pars) {
        return spuMapper.count(pars);
    }

    @Override
    public Integer updateStatus(Integer id, Integer status) {
        return spuMapper.updateStatus(id, status);
    }

    @Override
    public Integer update(Spu spu) {
        return spuMapper.update(spu);
    }

    @Override
    public Integer updateSku(Sku sku) {
        return skuMapper.update(sku);
    }

    @Override
    public Integer delSpu(Integer id) {
        return spuMapper.del(id);
    }

    @Override
    public Integer delSku(Integer id) {
        return skuMapper.del(id);
    }

    @Override
    public Integer add(Spu spu) {
        spuMapper.add(spu);
        Integer spuid = spu.getId();

        for (Sku sku : spu.getSkuList()){
            sku.setSpu_id(spuid);
            skuMapper.add(sku);
        }
        return 1;
    }

    @Override
    public List<Spu> all(Map<String, Object> pars) {
        List<Spu> spuList = spuMapper.query(pars);
        for (Spu spu : spuList) {
            spu.setSkuList(skuMapper.selectBySpuId(spu.getId()));
        }
        return spuList;
    }


}
